package com.haochang.chunkvod.controller.fragment.hostess;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.haochang.base.BaseActivity;
import com.haochang.base.BaseFragment;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.model.hostess.HostessData;

/**
 *         created at 2016/8/25 11:03
 *         清洁维修页面
 */
public class RoomClearFragment extends BaseFragment
{
	private String roomId;
	private String roomUsage;
	private String roomStatus;
	private HostessData hostessData;

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
	{
		View view = inflater.inflate(R.layout.fragment_room_clear, container, false);
		initView(view);
		return view;
	}

	private void initView(View view)
	{
		hostessData = new HostessData((BaseActivity) getActivity());
		Button room_clear_to_none = (Button) view.findViewById(R.id.room_clear_to_none);//转为空房
		Button room_clear_clear = (Button) view.findViewById(R.id.room_clear_clear);//转为清洁
		Button room_clear_repair = (Button) view.findViewById(R.id.room_clear_repair);//转为维修
		roomId = getArguments().getString(IntentKey.ROOM_ID);
		roomUsage = getArguments().getString(IntentKey.ROOM_USAGE);
		roomStatus = getArguments().getString(IntentKey.ROOM_STATUS);
		room_clear_to_none.setOnClickListener(onBaseClickListener);
		room_clear_clear.setOnClickListener(onBaseClickListener);
		room_clear_repair.setOnClickListener(onBaseClickListener);
	}

	private OnBaseClickListener onBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			if (!TextUtils.isEmpty(roomId))
			{
				switch (v.getId())
				{
				case R.id.room_clear_to_none:
					if (roomStatus.equals("empty"))
					{
						ToastUtils.showText(getActivity(), "该房间已是空房");
						return;
					}
					if (!TextUtils.isEmpty(roomUsage) && roomUsage.equals("sing"))
					{
						ToastUtils.showText(getActivity(), "房间已被分配，不可更改");
						return;
					}
					hostessData.setRoomToEmpty(roomId, new HostessData.NoticeListener()
					{
						@Override
						public void onRequestSuccess(String notice)
						{
							if (!TextUtils.isEmpty(notice))
							{
								ToastUtils.showText(getContext(), "状态修改成功");
								finishThenChangeState("empty");
							}
						}

						@Override
						public void onFail(int errno, String errMsg)
						{
							ToastUtils.showText(getActivity(), TextUtils.isEmpty(errMsg) ? "转为空房失败!" : errMsg);
						}
					});
					break;
				case R.id.room_clear_clear:
					if (roomStatus.equals("purifying"))
					{
						ToastUtils.showText(getActivity(), "该房间已是清洁状态");
						return;
					}
					hostessData.setRoomToClear(roomId, new HostessData.NoticeListener()
					{
						@Override
						public void onRequestSuccess(String notice)
						{
							if (!TextUtils.isEmpty(notice))
							{
								ToastUtils.showText(getContext(), "状态修改成功");
								finishThenChangeState("purifying");
							}
						}

						@Override
						public void onFail(int errno, String errMsg)
						{
							ToastUtils.showText(getActivity(), TextUtils.isEmpty(errMsg) ? "更改为 清洁房间状态 失败!" : errMsg);
						}
					});
					break;
				case R.id.room_clear_repair:
					if (roomStatus.equals("repairing"))
					{
						ToastUtils.showText(getActivity(), "该房间已是设备维修状态");
						return;
					}
					if (!TextUtils.isEmpty(roomUsage) && roomUsage.equals("sing"))
					{
						ToastUtils.showText(getActivity(), "房间已被分配，不可更改");
						return;
					}
					hostessData.setRoomToRepair(roomId, new HostessData.NoticeListener()
					{
						@Override
						public void onRequestSuccess(String notice)
						{
							if (!TextUtils.isEmpty(notice))
							{
								ToastUtils.showText(getContext(), "状态修改成功");
								finishThenChangeState("repairing");
							}
						}

						@Override
						public void onFail(int errno, String errMsg)
						{
							ToastUtils.showText(getActivity(), TextUtils.isEmpty(errMsg) ? "更改为 设备维修状态 失败!" : errMsg);
						}
					});
					break;
				}

			}
		}
	};

	/**
	 * 返回首页并改变状态 //先返回数据，本地先刷新，再网络刷新
	 * @param newState 新状态
	 */
	private void finishThenChangeState(String newState)
	{
		Intent intent = getActivity().getIntent();
		intent.putExtra(IntentKey.ROOM_ID, getArguments().getString(IntentKey.ROOM_ID));
		intent.putExtra(IntentKey.ROOM_STATUS, newState);
		getActivity().setResult(Activity.RESULT_OK, intent);//先返回数据，本地先刷新，再网络刷新
		getActivity().finish();
	}
}
